package com.seecen.HJM.controller;

import com.seecen.HJM.mapper.ArtistMapper;
import com.seecen.HJM.mapper.SongMapper;
import com.seecen.HJM.model.dto.*;
import com.seecen.HJM.model.entity.Artist;
import com.seecen.HJM.model.entity.Comment;
import com.seecen.HJM.model.entity.Playlist;
import com.seecen.HJM.model.vo.*;
import com.seecen.HJM.result.DataVo;
import com.seecen.HJM.result.PageResult;
import com.seecen.HJM.service.*;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author TOYA
 * @date 2025/9/26 15:09
 * @description: 管理员可以做的所有操作
 */

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {



    private final SongService songService;

    private final MinioService minioService;

    private final ArtistService artistService;

    private final PlaylistService playlistService;

    private final IUserService userService;

    private final ArtistMapper artistMapper;

    private final SongMapper songMapper;

    private final CommentService commentService;

    //添加歌曲信息  提交数据库信息和文件拆开做  未完成需前端配合
    @PostMapping("/addSong")
    public DataVo addSong(
            @RequestBody SongAddDto songAddDto
    ) {
        return songService.addSong(songAddDto);
    }

    //修改歌曲信息
    @PutMapping("/updateSong")
    public DataVo updateSong(@RequestBody SongUpdateDto songUpdateDto) {
        return songService.updateSong(songUpdateDto);
    }


    //上传歌曲封面
    @PatchMapping("/updateSongCover/{id}")
    public DataVo updateSongCover(
            @PathVariable("id") Long songId,
            @RequestParam("cover") MultipartFile cover
    ) {
        String coverUrl = minioService.uploadFile(cover,
                "songCovers");
        return songService.updateSongCover(songId, coverUrl);
    }

    //上传歌曲音频
    @PatchMapping("/updateSongAudio/{id}")
    public DataVo updateSongAudio(
            @PathVariable("id") Long songId,
            @RequestParam("audio") MultipartFile audio
    ) {
        String audioUrl = minioService.uploadFile(audio, "songs");  // 上传到 songs 目录
        return songService.updateSongAudio(songId, audioUrl);
    }

    //上传歌曲歌词
    @PatchMapping("/updateSongLyric/{id}")
    public DataVo updateSongLyric(
            @PathVariable("id") Long songId,
            @RequestParam("lyric") MultipartFile lyric
    ) {
        String lyricUrl = minioService.uploadFile(lyric, "lyrics");//上传到lyrics目录
        return songService.updateSongLyric(songId, lyricUrl);
    }

    //删除歌曲
    @DeleteMapping("/deleteSong/{id}")
    public DataVo deleteSong(
            @PathVariable("id") Long songId
    ) {
        return songService.deleteSong(songId);
    }

    //批量删除歌曲
    @DeleteMapping("/deleteSongs")
    public DataVo deleteSongs(@RequestBody List<Long> songIds) {
        return songService.deleteSongs(songIds);
    }

    //根据歌手id获取相关歌曲信息
    @PostMapping("/getAllSongsByArtist")
    public DataVo<PageResult<SongAdminVo>> getAllSongsByArtist(
            @RequestBody SongAndArtistDto songAndArtistDto
    ) {
        return songService.getAllSongsByArtist(songAndArtistDto);
    }

    //获取所有歌手id和名称
    @GetMapping("/getAllArtistNames")
    public DataVo<List<ArtistNameVo>> getAllArtistNames() {
        return artistService.getAllArtistNames();
    }

    //获取所有歌曲的数量，用于底部显示总条数
    @GetMapping("/getAllSongsCount")
    public DataVo<Long> getAllSongsCount(
            @RequestParam(required = false) String style
    ) {
        return songService.getAllSongsCount(style);
    }

    //获取所有歌手数量
    @GetMapping("/getCountOfArtist")
    public DataVo<Long> getCountOfArtist() {
        long count = artistService.count();
        return DataVo.success(count);
    }
    //获取所有歌单数量
    @GetMapping("/getCountOfPlayList")
    public DataVo<Long> getCountOfPlayList() {
        long count = playlistService.count();
        return DataVo.success(count);
    }
    //获取所有用户数量
    @GetMapping("/getCountOfUser")
    public DataVo<Long> getCountOfUser() {
        long count = userService.getTotalUsers();
        return DataVo.success(count);
    }
    //获取歌曲类型数据
    @GetMapping("song-type-stats")
    public DataVo getSongTypeStats() {
        List<SongStyleAndCountVo> songStyleAndCountVo = songMapper.getSongStyleAndCountVo();
        return DataVo.success(songStyleAndCountVo);
    }
    //获取歌手性别数据
    @GetMapping("artist-gender-stats")
    public DataVo getArtistGenderStats() {
        List<ArtistGenderAndCount> artistGenderAndCount = artistMapper.getArtistGenderAndCount();
        return DataVo.success(artistGenderAndCount);
    }
    //获取歌手地区分布数据
    @GetMapping("artist-country-stats")
    public DataVo getArtistAreaStats() {
        List<ArtistAreaAndCount> artistAreaAndCount = artistMapper.getArtistAreaAndCount();
        return DataVo.success(artistAreaAndCount);
    }
    //获取所有歌手信息

    @PostMapping("/getAllArtists")
    public DataVo<PageResult<Artist>> getAllArtists(@RequestBody ArtistDTO artistDTO) {
        return artistService.getAllArtistsAndDetail(artistDTO);
    }
    // 新增歌手
    @PostMapping("/addArtist")
    public DataVo addArtist(@RequestBody ArtistAddDTO artistAddDTO) {
        return artistService.addArtist(artistAddDTO);
    }

    // 更新歌手信息
    @PutMapping("/updateArtist")
    public DataVo  updateArtist(@RequestBody ArtistUpdateDTO artistUpdateDTO) {
        return artistService.updateArtist(artistUpdateDTO);
    }

    // 更新歌手头像
    @PatchMapping("/updateArtistAvatar/{id}")
    public DataVo updateArtistAvatar(@PathVariable("id") Long artistId, @RequestParam("avatar") MultipartFile avatar) {
        String avatarUrl = minioService.uploadFile(avatar, "artists");  // 上传到 artists 目录
        return artistService.updateArtistAvatar(artistId, avatarUrl);
    }

    // 删除歌手
    @DeleteMapping("/deleteArtist/{id}")
    public DataVo deleteArtist(@PathVariable("id") Long artistId) {
        return artistService.deleteArtist(artistId);
    }

    // 批量删除歌手
    @DeleteMapping("/deleteArtists")
    public DataVo deleteArtists(@RequestBody List<Long> artistIds) {
        return artistService.deleteArtists(artistIds);
    }


    //获取所有歌单信息
    @PostMapping("/getAllPlaylists")
    public DataVo<PageResult<Playlist>> getAllPlaylists(@RequestBody PlaylistDTO playlistDTO) {
        return playlistService.getAllPlaylistsInfo(playlistDTO);
    }
    //新增歌单
    @PostMapping("/addPlaylist")
    public DataVo addPlaylist(@RequestBody PlaylistAddDTO playlistAddDTO) {
        return playlistService.addPlaylist(playlistAddDTO);
    }

    // 更新歌单信息
    @PutMapping("/updatePlaylist")
    public DataVo updatePlaylist(@RequestBody PlaylistUpdateDTO playlistUpdateDTO) {
        return playlistService.updatePlaylist(playlistUpdateDTO);
    }

    //更新歌单封面
    @PatchMapping("/updatePlaylistCover/{id}")
    public DataVo updatePlaylistCover(@PathVariable("id") Long playlistId, @RequestParam("cover") MultipartFile cover) {
        String coverUrl = minioService.uploadFile(cover, "playlists");  // 上传到 playlists 目录
        return playlistService.updatePlaylistCover(playlistId, coverUrl);
    }

    //删除歌单
    @DeleteMapping("/deletePlaylist/{id}")
    public DataVo deletePlaylist(@PathVariable("id") Long playlistId) {
        return playlistService.deletePlaylist(playlistId);
    }

    //批量删除歌单
    @DeleteMapping("/deletePlaylists")
    public DataVo deletePlaylists(@RequestBody List<Long> playlistIds) {
        return playlistService.deletePlaylists(playlistIds);
    }

    //获取所有评论
    @PostMapping("/getComment")
    public DataVo<PageResult<Comment>> getAllComment(@RequestBody @Valid CommentDTO commentDTO) {
        return commentService.getAllComment(commentDTO);
    }

    // 批量删除评论（后台接口）
    @DeleteMapping("/deleteComments")
    public DataVo adminBatchDeleteComment(@RequestBody List<Long> commentIds) {
        // 调用服务层批量删除方法
        return commentService.adminDeleteComment(commentIds);
    }
}
